package com.ifreshwing.fragment;

import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.design.widget.CoordinatorLayout;
import android.support.v4.app.Fragment;
import android.support.v4.widget.SwipeRefreshLayout;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.ListView;
import android.widget.TextView;

import com.ifreshwing.adapter.HomeListAdapter;
import com.ifreshwing.bean.HomeRegionListBean;
import com.ifreshwing.socket.SocketConnect;
import com.ifreshwing.userpark.R;
import com.ifreshwing.utils.ToastUtil;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * Created by lz on 2017/4/26.
 * 首页fragment
 */
public class HomeFragment extends Fragment {
    @BindView(R.id.parking_lst)
    ListView parkingLst;
    @BindView(R.id.coor)
    CoordinatorLayout coor;
    @BindView(R.id.swipe_refresh)
    SwipeRefreshLayout swipeRefresh;
    Unbinder unbinder;
    @BindView(R.id.titleBar_text)
    TextView titleBarText;
    private View view;
    private Context context;
    private HomeListAdapter adapter;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_home, null, false);
        context = getActivity();
        unbinder = ButterKnife.bind(this, view);
        swipeRefresh.setOnRefreshListener(() -> new Handler().postDelayed(() -> {
            swipeRefresh.setRefreshing(false);
            handler.sendEmptyMessage(0);
        }, 1000));
        parkingLst.setOnScrollListener(new AbsListView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(AbsListView view, int scrollState) {

            }

            @Override
            public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
                swipeRefresh.setEnabled(firstVisibleItem == 0);
            }
        });
        titleBarText.setText("首页");
        getData();
        return view;
    }

    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            ToastUtil.showSnackBar(coor, "执行了刷新事件");

        }
    };

    /*模拟假数据*/
    private void getData() {
        List<HomeRegionListBean> list = new ArrayList<>();
        String[] str = {"A区", "B区", "C区", "D区", "E区"};
        for (int i = 0; i < 5; i++) {
            HomeRegionListBean bean = new HomeRegionListBean();
            bean.setParkingRegion_id(i + "");
            bean.setParkingRegion_name(str[i]);
            list.add(bean);
        }
        adapter = new HomeListAdapter(context, list);
        parkingLst.setAdapter(adapter);
        parkingLst.setOnItemClickListener((parent, view1, position, id) -> {
//            Intent intent = new Intent(getActivity(), ParkListActivity.class);
//            Bundle bundle = new Bundle();
//            bundle.putString("RegionName",list.get(position).getParkingRegion_name());
//            bundle.putString("RegionId",list.get(position).getParkingRegion_id());
//            intent.putExtras(bundle);
//            startActivity(intent);
            SocketConnect.getInstance().connect2server("LOGIN:123456");
        });
        adapter.notifyDataSetChanged();
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }
}
